﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IESN.BaseForms;
using IESN.BusinessCore;

namespace IESN
{
    /// <summary>
    /// 温度设定界面
    /// </summary>
    public partial class TemperatureSettingFrm :EditBaseFrm,IClient
    {
        #region 构造函数

        public TemperatureSettingFrm()
        {
            InitializeComponent();
            CurrentEntity = new Temperature();
        }

        public TemperatureSettingFrm(AppContext appContext,Controller controller)
            :this()
        {
            _appContext = appContext;
            _controller = controller;
        }

        #endregion

        #region 行切换

        /// <summary>
        /// 行切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvTemperature_SelectionChanged(object sender, EventArgs e)
        {
            CommonHelper<Temperature> cHelper = new CommonHelper<Temperature>();

            if (dgvTemperature.SelectedRows != null && dgvTemperature.SelectedRows.Count > 0)
            {
                DataGridViewRow gridrow = dgvTemperature.SelectedRows[0];
                DataRowView row_view = (DataRowView)gridrow.DataBoundItem;
                CurrentEntity = cHelper.FillModel(row_view.Row);

                ControlHelper.SetObjectValueToControl(CurrentEntity, panelContent);
            }
        }

        #endregion

        #region 操作完成后处理

        /// <summary>
        /// 新增后处理
        /// </summary>
        protected override void AfterInserted()
        {
            base.AfterInserted();
            InsertGrid();
        }

        /// <summary>
        /// 编辑后处理
        /// </summary>
        protected override void AfterUpDated()
        {
            base.AfterUpDated();
            UpdateGrid();
        }

        /// <summary>
        /// 插入到DataGrid
        /// </summary>
        private void InsertGrid()
        {
            BindingSource bs = dgvTemperature.DataSource as BindingSource;
            DataTable dt = bs.DataSource as DataTable;

            CommonHelper<Temperature> cHelper = new CommonHelper<Temperature>();
            DataRow newDr = cHelper.FillDataRow(CurrentEntity as Temperature, dt);
            DataTable cloneTb = dt.Copy();

            cloneTb.Rows.Add(newDr.ItemArray);

            BindingSource newBS = new BindingSource();
            newBS.DataSource = cloneTb;
            dgvTemperature.DataSource = newBS;
            dgvTemperature.Rows[cloneTb.Rows.Count - 1].Selected = true;
            dgvTemperature.Refresh();
        }

        /// <summary>
        /// 更新到DataGrid
        /// </summary>
        private void UpdateGrid()
        {
            BindingSource bs = dgvTemperature.DataSource as BindingSource;
            DataTable dt = bs.DataSource as DataTable;
            DataRow[] drs = dt.Select("ID='" + CurrentEntity.ID + "'");

            if (drs != null && drs.Length > 0)
            {
                DataRow dr = drs[0];

                CommonHelper<Temperature> cHelper = new CommonHelper<Temperature>();
                DataRow newDr = cHelper.FillDataRow(CurrentEntity as Temperature, dt);

                drs[0].ItemArray = newDr.ItemArray;
                dgvTemperature.Refresh();
            }
        }

        #endregion

        public void chineseVer()
        {
            throw new NotImplementedException();
        }

        public void englishVer()
        {
            throw new NotImplementedException();
        }

        public void failed(Exception e)
        {
            throw new NotImplementedException();
        }

        public void LockScreen()
        {
            throw new NotImplementedException();
        }

        public void ReadDM(DM.DmClass obj1)
        {
            MessageBox.Show(obj1.DMAddressName);
            //throw new NotImplementedException();
        }

        public void unlockScreen()
        {
            throw new NotImplementedException();
        }

        public void UpdatedPreset(string strRow, string strCol, string strTextValue)
        {
            throw new NotImplementedException();
        }
    }
}
